#include "Wrapper.h"
#include "BlueStore.h"

#include "common/global_init.h"
#include "common/global_context.h"
#include "common/ceph_argparse.h"
#include "common/errno.h"
#include "common/osd_types.h"
#include "common/Cond.h"
#include "include/uuid.h"

#define dout_context g_ceph_context
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif

struct Object 
{
	BlueStore *store;
};

struct Transaction 
{
	BlueStore *store;
	Transaction *trans;
};

struct Object *GetInstance(const char *path)
{
	struct Object *obj = (struct Object *) malloc(sizeof(Object));
	obj->store = (BlueStore *) malloc(sizeof(BlueStore));
	vector<const char *> args;
	env_to_vec(args);
	auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON, 0);
	common_init_finish(cct.get());
	// call bluestore create function
	BlueStore *store = new BlueStore(cct.get(), path);

	// check the store
	if (store) {
		obj->store = store;
		return obj;
	}
	return NULL;
}

void ReleaseInstance(struct Object **object)
{
	delete *object;
	*object = 0;
}

int mkfs(struct Object *object)
{
	// bluestore use fsid from /etc/ceph/conf file. Here generate
	//object->store->set_fsid(uuid.uuid);
	int ret = 0;
	ret = object->store->mkfs();
	if (ret) {
		printf("mkfs error.\n");
		exit(EXIT_FAILURE);
	}
	cout << "mkfs success." << std::endl;
	return ret;
}

#ifdef __cplusplus
};
#endif
